﻿using System.Windows ;
using System.Windows.Controls ;
using System.ServiceModel.DomainServices.Client.ApplicationServices ;
using System.Linq ;
using System.ComponentModel ;

namespace OpenLightGroup.Common.Interactivity
    {
        /// <summary>
        /// Toggles the visibility of a DataGridColumn based on the current user's roles.
        /// </summary>
        [Description( "Toggles the visibility of a DataGrid column based on the current user's roles." )]
        public class RoleBasedDataGridColumnVisibilityBehavior
        : BaseRoleBasedBehavior<DataGridColumn>
            {
                /// <summary>
                /// Updates the associated DataGridColumn
                /// </summary>
                /// <param name="sender">Not used.</param>
                /// <param name="e">Not used.</param>
                protected override void UpdateAssociatedObject ( object sender, AuthenticationEventArgs e )
                    {
                        if ( this.AllowedRoles.Where( r => this.IsInRole( r ) ).Count( ) > 0 )
                        {
                            this.AssociatedObject.Visibility = Visibility.Visible ;
                        }
                        else
                        {
                            this.AssociatedObject.Visibility = Visibility.Collapsed ;
                        }
                    }
            }
    }